<?php
namespace Home\Controller;
use Think\Controller\RestController;
/**
 * 首页控制器(包含登陆退出注册)
 * @author    王金鹏 <firKin@126.com>
 */
class IndexController extends RestController {


	public function index(){

		$dept = M('department')->where('dept_status = 1')->select();
		//dump($dept);die;
		$this->assign('dept', $dept);
		$this->display();
	}

	//登陆
    public function login(){
        if(!IS_POST) $this->error('页面不存在!');

      	$da = strip_tags($GLOBALS['HTTP_RAW_POST_DATA']);
      	$dd = json_decode($da, ture);
        $userStuNum = $dd['std_id'];			//账号是学号
        $pwd = md5($dd['std_psw']);			//密码身份证后六位
        $user = M('user')->where(array('user_stu_num' => $userStuNum))->find();
        if (!$user) {
        	send_http_status('403');
        	$this->ajaxReturn(array('error' => "学号错误!", 'target' => "std_id", 'state' => 403), 'json');
        }elseif ($user['user_password'] != $pwd) {
        	send_http_status('403');
        	$this->ajaxReturn(array('error' => "密码错误!", 'target' => "std_psw", 'state' => 403), 'json');
        }else{
        	session(C('USER_AUTH_KEY'), $user['user_id']);
			session('user_name', $user['user_name']);
			session('user_stu_num', $user['user_stu_num']);
			session('user_log_time', date('Y-m-d H:i:s', $user['user_log_time']));
			session('user_log_ip', $user['user_log_ip']);

			$uid = $user['user_id'];
			$role_id = M('user_dept_role')->where("user_id = $uid")->find('role_id');
        	$this->ajaxReturn(array('id' => $user['user_id'], 'nextSrc' => U('Home/Mission/index', '', ''), 'state' => 200), 'json');
        }
    }

    //退出登录
    public function logout(){
    	session_unset();
    	session_destroy();

    	$this->ajaxReturn(array(
    		'state' => 200,
    		'nextSrc' => U('Home/Index/index', '', '')
    		),'json');
    }

    //注册
    public function register(){
		if(!IS_POST) $this->error('页面不存在!');

		$da = strip_tags($GLOBALS['HTTP_RAW_POST_DATA']);
      	$dd = json_decode($da, true);
		$username = $dd['std_name'];
		$pwd = md5($dd['std_number']);			//身份证后六位是密码
		$userStuNum = $dd['std_id'];				//学号为账号
		$userDeptId = array();
		$userDeptId = $dd['std_department'];

		$user = M('think.stu', '')
					->where("stu_name = $username AND stu_num = $userStuNum AND ")
							->find();//测试
		$repeatUser = M('verify_user')->where("user_stu_num = $userStuNum")
									  ->find();
		$realUser = M('user')->where("user_stu_num = $userStuNum")
							 ->find();

		//判断 verify_user 表中是否存在该用户,避免重复注册
		if ($repeatUser) {
			send_http_status('403');
			$this->ajaxReturn(array('error' => '学号重复', 'state' => 403), 'json');
		}elseif (!$realUser) {	//判断 user 表中是否存在该用户, 避免重复

			//判断是否曾经有数据,如果有就直接插入到 user 表中,没有就插入到verify_user表中
			if ($user && $user['password'] = $pwd) {//测试

				$data = array(
					'user_name' => $username,
					'user_password' => $pwd,					//身份证后六位
					'user_stu_num' => $userStuNum,					//学号
					'user_log_time' => time(),
					'user_log_ip' => get_client_ip(),
					'user_status' => 1
				);
				$userId = M('user')->add($data);

				//添加部门和职务信息
				foreach ($userDeptId as $v) {
					$data1[] = array(
						'dept_id' => $v,
						'role_id' => '2',		//默认全部为学员
						'user_id' => $userId
						);
				}
				M('user_dept_role')->add($data1);
				$this->ajaxReturn(array('nextSrc' => U('Home/Index/login'), 'state' => 200), 'json');
			}

			$data = array(
				'user_name' => $username,
				'user_password' => $pwd,					//身份证后六位是密码
				'user_stu_num' => $userStuNum,				//学号为账号
				'user_status' => 1,
			);
			$userId = M('verify_user')->add($data);

			//添加部门和职务信息
			foreach ($userDeptId as $v) {
				$data1[] = array(
					'dept_id' => $v,
					'user_id' => $userId
					);
			}
			M('verify_user_dept')->addAll($data1);
			send_http_status('403');
			$this->ajaxReturn(array('error' => '待验证', 'state' => 403), 'json');
		}else{
			send_http_status('403');
			$this->ajaxReturn(array('error' => '用户已存在', 'state' => 403), 'json');
		}
	}
}